<?php
include( getcwd() . '/application/views/div/html_head_info.php' );
include( getcwd() . '/application/views/div/header.php' );
?>
<div class="container"> 
<div id="settings">

<p>
<?php include( getcwd() . '/application/views/div/user_settings_nav.php' ); ?>
</p>

<h2>
帐号设置
</h2>

<?php 
include( getcwd() . '/application/views/div/res_info.php' );
?>

<!--修改用户的帐号信息-->
<div id="account_settings">
<form action="/settings/do_modify_user_base_info" method="post" name="user_base_info">
<p>
	 <label>昵称</label>
	 <input type="text" class="text" maxlength="64" name="nick" value="<?php echo $local_subject_user_info['nick']; ?>"/>
</p>
<p>
	 <label>邮箱</label>
	 <input type="text" class="text" name="email" value="<?php echo $local_subject_user_info['email']; ?>"/>
</p>
<input type="submit" value="修改" />
</form>
</div>

<?php
//判断若用户还没有登录系统 则在点击购买按钮的时候会跳转
//到登录页面 ,当然 也不会显示地址等信息
if( !empty( $subject_user_info ) ){
?>
<p id="user_address">
	 <h3>收货地址</h3>
<?php 
//循环显示用户地址信息
if( !empty( $user_addresses ) ){ 
?>
	 <ul id="user_address_item">
<?php
foreach( $user_addresses as $no=>$address ){
?>
		  <li id="<?php echo $address['address_no']; ?>">	 
			  <div><?php echo $address['address']; ?></div>
			  <!--隐藏的地址信息-->
			  <div class="address_detail_for_modify">
				  <span class="city"><?php echo $address['city']; ?></span>
				  <span class="town"><?php echo $address['town']; ?></span>
				  <span class="province"><?php echo $address['province']; ?></span>
				  <span class="name"><?php echo $address['name']; ?></span>
				  <span class="address_detail"><?php echo $address['address_detail']; ?></span>
				  <span class="tel"><?php echo $address['tel']; ?></span>
				  <span class="zip"><?php echo $address['zip']; ?></span>
			  </div>
			  <div class="opt">
			 	   <span><a href="javascript:void();" class="modify_address">修改</a></span>
				   <span><a href="javascript:void();" class="delete_address">删除</a></span>
			  </div>
		  </li>
<?php
}
?>
	 </ul>
<?php
}
?>
	 <span id="add_new_address_button">添加新地址</span> 

	 <!--添加用户地址信息表单-->	 
	 <div id="add_new_address">
		 <form id="add_new_address_form" action="/settings/do_add_new_address_item" method="post">
		 <!--地址编号-->
		 <input type="hidden" name="address_no"/> 
		 <p>
			 <label>收货人 :</label>
			 <input type="text" class="text" maxlength="64" name="name"/>
		 </p>
		 <p id="province_city">
			 <label for="city" class="spc">省市区 :</label>
		 	 <select id="loc_province"  name="province" class="text input city"></select>
		 	 <input name="loc_province" type="hidden" />

		 	 <select id="loc_city" style="width:100px;" name="city" class="input text"></select>
		 	 <input name="loc_city" type="hidden" />

		 	 <select id="loc_town" style="width:122px;" name="town" class="input text"></select>
		 	 <input name="loc_town" type="hidden" />
		 </p>
		 <p>
			  <label>街道地址 :</label>
			  <input type="text" class="text" maxlength="64" name="address_detail"/>
		 </p>
		 <p>
			 <label>邮政编码 :</label>
			 <input type="text" class="text" maxlength="64" name="zip"/>
		 </p>
		 <p>
		 	 <label>电话号码 :</label>
			 <input type="text" class="text" maxlength="64" name="tel"/>
		 </p> 
		 </form>
		 <p>
		 	 <input class="buy_button" type="submit" value="添加"/>
	 	 </p>
	 </div>
</p>

</form>

<?php
}
?>

</div>
</div>
</div>

<script src="/js/libs/jquery_plugins/location/location.js" type="text/javascript"></script>
<script src="/js/libs/jquery_plugins/location/area.js" type="text/javascript"></script>
<script src="/js/address.js" type="text/javascript"></script>

<script type="text/javascript">
(function(){
	//使能添加新的地址时的提交按钮
	$( '.buy_button' ).click( function(){

		 $( '#add_new_address_form' ).submit();
	});
	//使能删除按钮
	$( '.delete_address' ).each( function( index , el ){
	
		var this_el = $( el );
		//当前点击 address 的 item
		var this_item = this_el.parent().parent().parent();
		//当前 address id
		var this_address_no = this_item.attr( 'id' );

		this_el.click( function(){
			$.ajax({
				 type : 'POST',
				 dataType : 'json',
				 url : '/settings/do_delete_address_item',
				 data : "address_no=" + this_address_no ,
				 success : function( res ){
		
					 if( res.res == "TRUE" ){
			 
						 //删除成功
						 this_item.remove();
						 //如果已经没有了地址信息
						 //则 删除地址显示框
						 if( $( '#user_address_item li' )[0] == undefined ){
							 
							 $( '#user_address_item' ).hide();
						 };
					 }else{
					 
						 alert( 'false oops!' );
					 }
				 },
				 error : function(){
				 
					 alert( 'oops!' );
				 }
			});
		});
		
	});

	//使能修改按钮
	$( '.modify_address' ).each( function( index , el ){

		var this_el = $( el );
		//当前点击 address 的 item
		var this_item = this_el.parent().parent().parent();
		//当前 address_no
		var this_address_no = this_item.attr( 'id' );
		
		this_el.click( function(){
			
			//读取改地址的信息 传递到修改表单
			var province = this_item.find( '.province' ).text();
			var city = this_item.find( '.city' ).text();
			var town = this_item.find( '.town' ).text();

			var name = this_item.find( '.name' ).text();
			var address_detail = this_item.find( '.address_detail' ).text();
			var zip = this_item.find( '.zip' ).text();
			var tel = this_item.find( '.tel' ).text();

			var modify_address_form = $( '#add_new_address' );

			showLocation();

			modify_address_form.find( 'select[name=province]' ).val( province );
			modify_address_form.find( 'select[name=province]' ).trigger( 'change' );
			modify_address_form.find( 'select[name=city]' ).val( city );
			modify_address_form.find( 'select[name=city]' ).trigger( 'change' );
			modify_address_form.find( 'select[name=town]' ).val( town );
			modify_address_form.find( 'select[name=town]' ).trigger( 'change' );

			//修改地址信息需要指定的地址编号信息
			modify_address_form.find( 'input[name=address_no]' ).val( this_address_no );

			modify_address_form.find( 'input[name=name]' ).val( name );
			modify_address_form.find( 'input[name=address_detail]' ).val( address_detail );
			modify_address_form.find( 'input[name=zip]' ).val( zip );
			modify_address_form.find( 'input[name=tel]' ).val( tel );
			
			modify_address_form.show();
		});
	});
})();
</script>

<?php
include( getcwd().'/application/views/div/footer.php' );
?>

